package com.jason.fontmetrics.view;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * @DESC: BlurMaskFilter只有一个含参的构造函数BlurMaskFilter(float radius, BlurMaskFilter.Blur style)，
 *        其中radius很容易理解，值越大我们的阴影越扩散，比如在上面的例子中我将radius改为50
 *          而第二个参数style表示的是模糊的类型，上面我们用到的是SOLID，其效果就是在图像的Alpha边界外产生一层与Paint颜色一致的阴影效果而不影响图像本身，
 *          除了SOLID还有三种，NORMAL,OUTER和INNER,
 *              1.NORMAL会将整个图像模糊掉
 *              2.OUTER会在Alpha边界外产生一层阴影且会将原本的图像变透明
 *              3.INNER则会在图像内部产生模糊：
 * @Author: Jason
 * @Date: 16/8/29
 * @Time: 22:17
 */

public class MaskFilterView4 extends View {

    private static final int RECT_SIZE = 800;

    private Paint mPaint;

    private Context mContext;

    private int left,top,right,bottom;


    public MaskFilterView4(Context context) {
        this(context, null);

    }

    public MaskFilterView4(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        // 初始化画笔
        initPaint();
        // 初始化资源
        initRes(context);

        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }

    private void initRes(Context context) {

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;
        // 计算位图绘制时左上角的坐标使其位于屏幕中心
        left = width / 2 - RECT_SIZE / 2;
        top = height / 2 - RECT_SIZE / 2;
        right = width / 2 + RECT_SIZE / 2;
        bottom = height / 2 + RECT_SIZE / 2;

    }

    private void initPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(0xFF603811);

        // 设置画笔遮罩滤镜
        mPaint.setMaskFilter(new BlurMaskFilter(100, BlurMaskFilter.Blur.OUTER));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.GRAY);

        // 画矩形
        canvas.drawRect(left, top, right, bottom, mPaint);
    }
}
